﻿using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLayer
{
    public class ProductBusiness
    {

        public static List<VDataEntity> GetProductCategoryList()
        {
            using (TrialManagementEntities trEntity = new TrialManagementEntities())
            {
                var productCategoryList = from productCategory in trEntity.ProductCategory
                                          where productCategory.IsActive == true
                                          select new VDataEntity() { ID = productCategory.ID, Name = productCategory.Name };
                return productCategoryList.ToList();
            }
        }

        public static VProduct[] GetActiveProductListByProductCategory(long productCategoryID)
        {
            using (TrialManagementEntities trEntity = new TrialManagementEntities())
            {
                var productList = from product in trEntity.Product
                                  where product.IsActive == true && product.CategoryID == productCategoryID
                                  select new VProduct() { ID = product.ID, IsActive = true, IsOther = product.IsOthers, Name = product.Name };
                return productList.ToArray();
            }
        }

        public static VRichProduct[] GetAllProductListWithoutOthers()
        {
            using (TrialManagementEntities trEntity = new TrialManagementEntities())
            {
                var productList = from product in trEntity.Product
                                  join productCategory in trEntity.ProductCategory on product.CategoryID equals productCategory.ID
                                  where product.IsOthers == false
                                  select new VRichProduct() { ID = product.ID, Name = product.Name, IsActive = product.IsActive, IsOther = product.IsOthers, CategoryID = product.CategoryID, CategoryName = productCategory.Name};
                return productList.ToArray();
            }

        }

        public static void AddProduct(string Name, long categoryID)
        {
            using(TrialManagementEntities trEntity = new TrialManagementEntities())
            {
                trEntity.Product.Add(new Product() { Name = Name, IsActive = true, IsOthers = false, CategoryID = categoryID });
                trEntity.SaveChanges();
            }
        }

        public static void ChangeStatus(long productID)
        {
            using (TrialManagementEntities trEntity = new TrialManagementEntities())
            {
                var product = (from p in trEntity.Product
                               where p.ID == productID
                               select p).FirstOrDefault();
                if (product != null)
                {
                    bool status = !product.IsActive;
                    product.IsActive = status;
                    trEntity.SaveChanges();
                }

            }
        }
    }
}
